package org.example.controller;
import org.example.entity.ResultsInfoEntity;
import org.example.service.AutoGradeService;
import org.example.service.ResultsInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/results")
public class ResultsInfoController {
    @Qualifier("resultServiceImpl")
    @Autowired
    private ResultsInfoService resultService;
    private AutoGradeService autoGradeService;
    // 根据考试ID,学生id查询结果
    @GetMapping("resultsByExamId&StudentId")
    public List<ResultsInfoEntity> getResultsByExamAndStudent(@RequestParam long examId, @RequestParam long studentId) {
        return resultService.getResultsByESId(examId, studentId);
    }
    //根据问题ID,学生ID,查找结果
    @GetMapping("resultsByQuesId&StudentId")
    public ResultsInfoEntity getResultsByQuestionAndStudent(@RequestParam long questionId, @RequestParam long studentId) {
        return resultService.getResultsByQuestionId(questionId, studentId);
    }
    // 添加或更新结果
    @PostMapping("saveResults")
    public ResponseEntity<String> saveResult(@RequestBody ResultsInfoEntity result) {
        boolean success = resultService.saveResult(result);
        return success
                ? ResponseEntity.ok("Result saved successfully.")
                : ResponseEntity.status(500).body("Failed to save result.");
    }

    // 根据ID删除结果
    @DeleteMapping("/{resultId}")
    public ResponseEntity<String> deleteResultById(@PathVariable Integer resultId) {
        boolean success = resultService.deleteResultById(resultId);
        return success
                ? ResponseEntity.ok("Result deleted successfully.")
                : ResponseEntity.status(500).body("Failed to delete result.");
    }
    @PostMapping("/grade")
    public float gradeExam(@RequestParam int examId, @RequestParam int studentId) {
       return autoGradeService.autoGradeExam(examId, studentId);

    }
}
